package com.champrin.replicationofmyblockapix.activity;

import static com.champrin.replicationofmyblockapix.http.HTTPUtils.getStringByStream;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.champrin.replicationofmyblockapix.R;
import com.champrin.replicationofmyblockapix.http.Player;
import com.champrin.replicationofmyblockapix.http.UserResponse;
import com.google.gson.Gson;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;


public class RegisterActivity extends AppCompatActivity {

    private EditText editTextUsername;
    private EditText editTextPassword;
    private EditText editTextEmail;
    private CheckBox checkBoxAgree;
    private Button buttonRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        // 初始化界面元素
        editTextUsername = findViewById(R.id.et_username_register);
        editTextPassword = findViewById(R.id.et_password_registers);
        editTextEmail = findViewById(R.id.et_email_register);
        checkBoxAgree = findViewById(R.id.cb_register);
        buttonRegister = findViewById(R.id.button_commit_register);

        // 注册按钮点击事件
        buttonRegister.setOnClickListener(v -> {
            String username = editTextUsername.getText().toString();
            String password = editTextPassword.getText().toString();
            String email = editTextEmail.getText().toString();

            if (username.isEmpty() || password.isEmpty() || email.isEmpty()) {
                Toast.makeText(RegisterActivity.this, "请输入用户名、密码和邮箱", Toast.LENGTH_SHORT).show();
            } else if (!checkBoxAgree.isChecked()) {
                Toast.makeText(RegisterActivity.this, "请同意用户协议", Toast.LENGTH_SHORT).show();
            } else {
                register(username, password, email);
            }
        });
    }

    // 执行注册逻辑
    private void register(String username, String password, String email) {
        registerResult(username, password, email);
        if (registerResult != null && registerResult.equals("Success")) {

            // 注册成功后的处理
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(RegisterActivity.this, MenuActivity.class);
            intent.putExtra("message", "register");
            startActivity(intent);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
    }

    private String registerResult = null;

    private void registerResult(String username, String password, String email) {
        new Thread() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                UserResponse userResponse = null;
                String result = null;
                try {
                    String path = "http://10.33.73.41:8080/register?" + "username=" + username + "&password=" + password;
                    Log.v("d", path);
                    URL url = new URL(path);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(6000);
                    connection.setReadTimeout(6000);
                    //设置请求方式 GET / POST 一定要大小
                    connection.setRequestMethod("GET");
                    connection.setDoInput(true);
                    connection.setDoOutput(false);
                    connection.connect();
                    int responseCode = connection.getResponseCode();
                    if (responseCode != HttpURLConnection.HTTP_OK) {
                        throw new IOException("HTTP error code" + responseCode);
                    }
                    result = getStringByStream(connection.getInputStream());
                    Log.d("d", result);
                    if (result != null) {
                        userResponse = new Gson().fromJson(result, UserResponse.class);
                        Player player = userResponse.getData();
                        if (player != null) {
                            registerResult = userResponse.getMsg();
                            Player.globolPlayer = player;
                            Log.d("d", Player.globolPlayer.toString());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}